//
//  PrCodeView.m
//  BuyWhat
//
//  Created by ios on 2016/11/1.
//  Copyright © 2016年 吕金状. All rights reserved.
//

#import "PrCodeView.h"
//#import "CreatQRCodeAndBarCodeFromLeon.h"

@interface PrCodeView ()

@property (nonatomic , strong) UIButton *cancelButton;
@property (nonatomic , strong) UIView *bgView;
@property (nonatomic , strong) UIImageView *prCodeImageView;

@end

@implementation PrCodeView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:backView];
        if (self.nameString != NULL) {
            _bgView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 235 / WIDTH_5S_SCALE) /2, (SCREEN_HEIGHT  - 310 / WIDTH_5S_SCALE) / 2, 235 / WIDTH_5S_SCALE, 310 / WIDTH_5S_SCALE)];
        }else{
            _bgView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 235 / WIDTH_5S_SCALE) /2, (SCREEN_HEIGHT  - 310 / WIDTH_5S_SCALE) / 2, 235 / WIDTH_5S_SCALE, 280 / WIDTH_5S_SCALE)];
        }
        
        _bgView.backgroundColor = getColor(whiteColor);
        [self addSubview:_bgView];
        
        _cancelButton = [[UIButton alloc]init];
        [_cancelButton setImage:[UIImage imageNamed:@"button_Deletepage"] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:_cancelButton];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(3 / WIDTH_5S_SCALE);
            make.right.equalTo(self.bgView).offset(- 3 / WIDTH_5S_SCALE);
            make.width.mas_offset(20 / WIDTH_5S_SCALE);
            make.height.mas_offset(20 / WIDTH_5S_SCALE);
        }];
        
        _prCodeImageView = [[UIImageView alloc]initWithFrame:CGRectMake((235 / WIDTH_5S_SCALE - 200 / WIDTH_5S_SCALE) / 2, 28 / WIDTH_5S_SCALE, 200 / WIDTH_5S_SCALE, 200 / WIDTH_5S_SCALE)];

        //_prCodeImageView.image = [CreatQRCodeAndBarCodeFromLeon qrImageWithString:@"http://www.wingfac.com/xct/product.html" size:200 / WIDTH_5S_SCALE color:getColor(blueColor) icon:[UIImage imageNamed:@"qrCodeImageView"] iconWidth:50 / WIDTH_5S_SCALE];

        [_bgView addSubview:_prCodeImageView];
        
        
        
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.text = [NSString stringWithFormat:@"%@邀请您安装买啥",self.nameString];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        //nameLabel.textColor = getColor(darkGrey);
        nameLabel.font = DEF_FontSize_15;
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.text = @"扫一扫上方二维码安装";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        //titleLabel.textColor = getColor(darkGrey);
        titleLabel.font = DEF_FontSize_15;
        
        if (self.nameString != NULL) {
            [_bgView addSubview:nameLabel];
            [_bgView addSubview:titleLabel];
            [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.prCodeImageView.mas_bottom).offset(15 / WIDTH_5S_SCALE);
                make.left.equalTo(self.bgView).offset(0);
                make.height.mas_offset(15 / WIDTH_5S_SCALE);
                make.width.mas_offset(235 / WIDTH_5S_SCALE);
            }];
            
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(nameLabel.mas_bottom).offset(15 / WIDTH_5S_SCALE);
                make.left.equalTo(self.bgView).offset(0);
                make.height.mas_offset(15 / WIDTH_5S_SCALE);
                make.width.mas_offset(235 / WIDTH_5S_SCALE);
            }];
        }else{
            [_bgView addSubview:titleLabel];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.prCodeImageView.mas_bottom).offset(15 / WIDTH_5S_SCALE);
                make.left.equalTo(self.bgView).offset(0);
                make.height.mas_offset(15 / WIDTH_5S_SCALE);
                make.width.mas_offset(235 / WIDTH_5S_SCALE);
            }];
        }
        
    }
    return self;
}

//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
